#include <QApplication>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QGroupBox>
#include <QLabel>
#include <QPushButton>
#include <QDebug>
#include <QSpinBox>
#include <QCheckBox>
#include <QComboBox>
#include <QRandomGenerator>


#include "ElaApplication.h"
#include "ElaTheme.h"
#include "ElaWidget.h"
#include "ElaSpinBox.h"
#include "ElaDoubleSpinBox.h"

class ElaSpinBoxDemo : public ElaWidget
{
    Q_OBJECT

public:
    ElaSpinBoxDemo(QWidget *parent = nullptr) : ElaWidget(parent)
    {
        setWindowTitle("ElaSpinBox 功能演示");
        resize(800, 700);
        setWindowButtonFlags(ElaAppBarType::StayTopButtonHint | 
                           ElaAppBarType::MinimizeButtonHint | 
                           ElaAppBarType::MaximizeButtonHint | 
                           ElaAppBarType::CloseButtonHint | 
                           ElaAppBarType::ThemeChangeButtonHint | 
                           ElaAppBarType::NavigationButtonHint);

        setupUI();
        setupConnections();
    }

private:
    void setupUI()
    {
        QVBoxLayout *mainLayout = new QVBoxLayout(this);

        // 1. 按钮模式展示
        mainLayout->addWidget(createButtonModeGroup());

        // 2. 基础功能演示
        mainLayout->addWidget(createBasicFunctionGroup());

        // 3. 范围与步长设置
        mainLayout->addWidget(createRangeStepGroup());

        // 4. 特殊功能演示
        mainLayout->addWidget(createSpecialFeaturesGroup());

        // 5. 动态操作演示
        mainLayout->addWidget(createDynamicOperationGroup());

        mainLayout->addStretch();
    }

    // 创建按钮模式展示组
    QGroupBox *createButtonModeGroup()
    {
        QGroupBox *group = new QGroupBox("按钮模式展示", this);
        QVBoxLayout *layout = new QVBoxLayout(group);

        QLabel *infoLabel = new QLabel("展示不同的按钮布局模式", this);
        layout->addWidget(infoLabel);

        // 模式选择
        QHBoxLayout *modeSelectionLayout = new QHBoxLayout();
        QLabel *modeLabel = new QLabel("选择按钮模式:", this);
        QComboBox *modeComboBox = new QComboBox(this);
        modeComboBox->addItem("Inline - 内联模式", ElaSpinBoxType::Inline);
        modeComboBox->addItem("Compact - 紧凑模式", ElaSpinBoxType::Compact);
        modeComboBox->addItem("Side - 侧边模式", ElaSpinBoxType::Side);
        modeComboBox->addItem("PMSide - PM侧边模式", ElaSpinBoxType::PMSide);

        modeSelectionLayout->addWidget(modeLabel);
        modeSelectionLayout->addWidget(modeComboBox);
        modeSelectionLayout->addStretch();

        // 不同模式的展示
        m_inlineSpinBox = new ElaSpinBox(this);
        m_inlineSpinBox->setButtonMode(ElaSpinBoxType::Inline);
        m_inlineSpinBox->setValue(50);
        m_inlineSpinBox->setPrefix("Inline: ");

        m_compactSpinBox = new ElaSpinBox(this);
        m_compactSpinBox->setButtonMode(ElaSpinBoxType::Compact);
        m_compactSpinBox->setValue(100);
        m_compactSpinBox->setSuffix(" px");

        m_sideSpinBox = new ElaSpinBox(this);
        m_sideSpinBox->setButtonMode(ElaSpinBoxType::Side);
        m_sideSpinBox->setValue(200);
        m_sideSpinBox->setPrefix("W: ");

        m_pmSideSpinBox = new ElaSpinBox(this);
        m_pmSideSpinBox->setButtonMode(ElaSpinBoxType::PMSide);
        m_pmSideSpinBox->setValue(75);
        m_pmSideSpinBox->setSuffix(" %");

        layout->addLayout(modeSelectionLayout);
        layout->addWidget(new QLabel("内联模式 (Inline):", this));
        layout->addWidget(m_inlineSpinBox);
        layout->addWidget(new QLabel("紧凑模式 (Compact):", this));
        layout->addWidget(m_compactSpinBox);
        layout->addWidget(new QLabel("侧边模式 (Side):", this));
        layout->addWidget(m_sideSpinBox);
        layout->addWidget(new QLabel("PM侧边模式 (PMSide):", this));
        layout->addWidget(m_pmSideSpinBox);

        m_modeComboBox = modeComboBox;
        m_modeSpinBoxes << m_inlineSpinBox << m_compactSpinBox << m_sideSpinBox << m_pmSideSpinBox;

        return group;
    }

    // 创建基础功能演示组
    QGroupBox *createBasicFunctionGroup()
    {
        QGroupBox *group = new QGroupBox("基础功能演示", this);
        QVBoxLayout *layout = new QVBoxLayout(group);

        QLabel *infoLabel = new QLabel("展示SpinBox的基本功能", this);
        layout->addWidget(infoLabel);

        // 前缀后缀设置
        QHBoxLayout *prefixSuffixLayout = new QHBoxLayout();
        m_prefixSpinBox = new ElaSpinBox(this);
        m_prefixSpinBox->setPrefix("价格: $");
        m_prefixSpinBox->setValue(99);

        m_suffixSpinBox = new ElaSpinBox(this);
        m_suffixSpinBox->setSuffix(" 公斤");
        m_suffixSpinBox->setValue(5);

        prefixSuffixLayout->addWidget(m_prefixSpinBox);
        prefixSuffixLayout->addWidget(m_suffixSpinBox);
        prefixSuffixLayout->addStretch();

        // 特殊数值显示
        QHBoxLayout *specialValueLayout = new QHBoxLayout();
        m_specialValueSpinBox = new ElaSpinBox(this);
        m_specialValueSpinBox->setSpecialValueText("未设置");
        m_specialValueSpinBox->setMinimum(0);
        m_specialValueSpinBox->setMaximum(100);
        m_specialValueSpinBox->setValue(0);

        specialValueLayout->addWidget(new QLabel("特殊值显示:", this));
        specialValueLayout->addWidget(m_specialValueSpinBox);
        specialValueLayout->addStretch();

        // 只读状态
        QHBoxLayout *readOnlyLayout = new QHBoxLayout();
        m_readOnlySpinBox = new ElaSpinBox(this);
        m_readOnlySpinBox->setValue(42);
        m_readOnlySpinBox->setReadOnly(true);

        readOnlyLayout->addWidget(new QLabel("只读状态:", this));
        readOnlyLayout->addWidget(m_readOnlySpinBox);
        readOnlyLayout->addStretch();

        // 禁用状态
        QHBoxLayout *disabledLayout = new QHBoxLayout();
        m_disableSpinBox = new ElaSpinBox(this);
        m_disableSpinBox->setValue(42);
        m_disableSpinBox->setEnabled(false);
        disabledLayout->addWidget(new QLabel("禁用状态:", this));
        disabledLayout->addWidget(m_disableSpinBox);
        disabledLayout->addStretch();


        layout->addLayout(prefixSuffixLayout);
        layout->addLayout(specialValueLayout);
        layout->addLayout(readOnlyLayout);
        layout->addLayout(disabledLayout);

        m_basicSpinBoxes << m_prefixSpinBox << m_suffixSpinBox << m_specialValueSpinBox << m_readOnlySpinBox;

        return group;
    }

    // 创建范围与步长设置组
    QGroupBox *createRangeStepGroup()
    {
        QGroupBox *group = new QGroupBox("范围与步长设置", this);
        QVBoxLayout *layout = new QVBoxLayout(group);

        QLabel *infoLabel = new QLabel("展示数值范围和步长设置", this);
        layout->addWidget(infoLabel);

        // 范围设置
        QHBoxLayout *rangeLayout = new QHBoxLayout();
        m_rangeSpinBox = new ElaSpinBox(this);
        m_rangeSpinBox->setRange(0, 1000);  // 设置范围 0-1000
        m_rangeSpinBox->setValue(500);


        rangeLayout->addWidget(new QLabel("范围 0-1000:", this));
        rangeLayout->addWidget(m_rangeSpinBox);
        rangeLayout->addStretch();

        m_doubleRangeSpinBox = new ElaDoubleSpinBox(this);
        m_doubleRangeSpinBox->setRange(0.0, 1000.0);  // 设置范围 0.0-1000.
        m_doubleRangeSpinBox->setValue(500.0);
        m_doubleRangeSpinBox->setDecimals(2);  // 设置小数点后两位
        m_doubleRangeSpinBox->setSingleStep(0.1);  // 设置步长为0.1
        m_doubleRangeSpinBox->setAccelerated(true);  // 启用加速
        m_doubleRangeSpinBox->setMinimumSize(200, 40);


        rangeLayout->addWidget(new QLabel("范围 0.0-1000.0:", this));
        rangeLayout->addWidget(m_doubleRangeSpinBox);
        rangeLayout->addStretch();

        // 步长设置
        QHBoxLayout *stepLayout = new QHBoxLayout();
        m_stepSpinBox = new ElaSpinBox(this);
        m_stepSpinBox->setSingleStep(10);  // 设置步长为10
        m_stepSpinBox->setValue(100);

        stepLayout->addWidget(new QLabel("步长 10:", this));
        stepLayout->addWidget(m_stepSpinBox);
        stepLayout->addStretch();

        // 加速设置
        QHBoxLayout *accelerationLayout = new QHBoxLayout();
        m_accelerationSpinBox = new ElaSpinBox(this);
        m_accelerationSpinBox->setAccelerated(true);  // 启用加速
        m_accelerationSpinBox->setRange(1, 10000);
        m_accelerationSpinBox->setValue(1000);

        accelerationLayout->addWidget(new QLabel("启用加速:", this));
        accelerationLayout->addWidget(m_accelerationSpinBox);
        accelerationLayout->addStretch();

        layout->addLayout(rangeLayout);
        layout->addLayout(stepLayout);
        layout->addLayout(accelerationLayout);

        m_rangeStepSpinBoxes << m_rangeSpinBox << m_stepSpinBox << m_accelerationSpinBox;

        return group;
    }

    // 创建特殊功能演示组
    QGroupBox *createSpecialFeaturesGroup()
    {
        QGroupBox *group = new QGroupBox("特殊功能演示", this);
        QVBoxLayout *layout = new QVBoxLayout(group);

        QLabel *infoLabel = new QLabel("展示特殊功能和状态", this);
        layout->addWidget(infoLabel);

        // 包装模式
        QHBoxLayout *wrappingLayout = new QHBoxLayout();
        m_wrappingSpinBox = new ElaSpinBox(this);
        m_wrappingSpinBox->setWrapping(true);  // 启用包装
        m_wrappingSpinBox->setRange(1, 10);
        m_wrappingSpinBox->setValue(5);

        wrappingLayout->addWidget(new QLabel("循环包装模式:", this));
        wrappingLayout->addWidget(m_wrappingSpinBox);
        wrappingLayout->addStretch();

        // 键盘跟踪
        QHBoxLayout *keyboardTrackingLayout = new QHBoxLayout();
        m_keyboardTrackingSpinBox = new ElaSpinBox(this);
        m_keyboardTrackingSpinBox->setKeyboardTracking(false);  // 禁用键盘跟踪
        m_keyboardTrackingSpinBox->setRange(0, 1000);
        m_keyboardTrackingSpinBox->setValue(100);

        keyboardTrackingLayout->addWidget(new QLabel("禁用键盘跟踪:", this));
        keyboardTrackingLayout->addWidget(m_keyboardTrackingSpinBox);
        keyboardTrackingLayout->addStretch();

        // 对齐方式
        QHBoxLayout *alignmentLayout = new QHBoxLayout();
        m_alignmentSpinBox = new ElaSpinBox(this);
        m_alignmentSpinBox->setAlignment(Qt::AlignRight | Qt::AlignVCenter);
        m_alignmentSpinBox->setValue(999);

        alignmentLayout->addWidget(new QLabel("右对齐:", this));
        alignmentLayout->addWidget(m_alignmentSpinBox);
        alignmentLayout->addStretch();

        layout->addLayout(wrappingLayout);
        layout->addLayout(keyboardTrackingLayout);
        layout->addLayout(alignmentLayout);

        m_specialSpinBoxes << m_wrappingSpinBox << m_keyboardTrackingSpinBox << m_alignmentSpinBox;

        return group;
    }

    // 创建动态操作演示组
    QGroupBox *createDynamicOperationGroup()
    {
        QGroupBox *group = new QGroupBox("动态操作演示", this);
        QVBoxLayout *layout = new QVBoxLayout(group);

        QLabel *infoLabel = new QLabel("演示SpinBox的动态操作和信号", this);
        layout->addWidget(infoLabel);

        // 动态修改属性
        QHBoxLayout *dynamicPropertyLayout = new QHBoxLayout();
        m_dynamicSpinBox = new ElaSpinBox(this);
        m_dynamicSpinBox->setValue(50);
        
        QPushButton *changePrefixBtn = new QPushButton("修改前缀", this);
        QPushButton *changeSuffixBtn = new QPushButton("修改后缀", this);
        QPushButton *toggleReadOnlyBtn = new QPushButton("切换只读", this);

        dynamicPropertyLayout->addWidget(new QLabel("目标SpinBox:", this));
        dynamicPropertyLayout->addWidget(m_dynamicSpinBox);
        dynamicPropertyLayout->addWidget(changePrefixBtn);
        dynamicPropertyLayout->addWidget(changeSuffixBtn);
        dynamicPropertyLayout->addWidget(toggleReadOnlyBtn);
        dynamicPropertyLayout->addStretch();

        // 数值操作
        QHBoxLayout *valueOperationLayout = new QHBoxLayout();
        QPushButton *setMinBtn = new QPushButton("设为最小值", this);
        QPushButton *setMaxBtn = new QPushButton("设为最大值", this);
        QPushButton *setZeroBtn = new QPushButton("设为0", this);
        QPushButton *randomValueBtn = new QPushButton("随机值", this);

        valueOperationLayout->addWidget(new QLabel("数值操作:", this));
        valueOperationLayout->addWidget(setMinBtn);
        valueOperationLayout->addWidget(setMaxBtn);
        valueOperationLayout->addWidget(setZeroBtn);
        valueOperationLayout->addWidget(randomValueBtn);
        valueOperationLayout->addStretch();

        // 信号状态显示
        QHBoxLayout *statusLayout = new QHBoxLayout();
        m_statusDisplay = new QLabel("当前值: 50", this);
        m_signalDisplay = new QLabel("最后信号: 无", this);

        statusLayout->addWidget(m_statusDisplay);
        statusLayout->addWidget(m_signalDisplay);
        statusLayout->addStretch();

        layout->addLayout(dynamicPropertyLayout);
        layout->addLayout(valueOperationLayout);
        layout->addLayout(statusLayout);

        m_operationButtons << changePrefixBtn << changeSuffixBtn << toggleReadOnlyBtn 
                          << setMinBtn << setMaxBtn << setZeroBtn << randomValueBtn;

        return group;
    }

    void setupConnections()
    {
        // 模式切换
        connect(m_modeComboBox, QOverload<int>::of(&QComboBox::currentIndexChanged), this, [this](int index) {
            ElaSpinBoxType::ButtonMode mode = static_cast<ElaSpinBoxType::ButtonMode>(
                m_modeComboBox->itemData(index).toInt());
            
            for (ElaSpinBox* spinBox : m_modeSpinBoxes) {
                spinBox->setButtonMode(mode);
            }
            
            qDebug() << "所有SpinBox切换到模式:" << m_modeComboBox->itemText(index);
        });

        // 动态属性修改
        connect(m_operationButtons[0], &QPushButton::clicked, this, [this]() {
            static int prefixCount = 0;
            prefixCount++;
            QString newPrefix = QString("前缀%1: ").arg(prefixCount);
            m_dynamicSpinBox->setPrefix(newPrefix);
            qDebug() << "前缀修改为:" << newPrefix;
        });

        connect(m_operationButtons[1], &QPushButton::clicked, this, [this]() {
            static int suffixCount = 0;
            suffixCount++;
            QString newSuffix = QString(" 单位%1").arg(suffixCount);
            m_dynamicSpinBox->setSuffix(newSuffix);
            qDebug() << "后缀修改为:" << newSuffix;
        });

        connect(m_operationButtons[2], &QPushButton::clicked, this, [this]() {
            bool currentState = m_dynamicSpinBox->isReadOnly();
            m_dynamicSpinBox->setReadOnly(!currentState);
            QString stateText = !currentState ? "只读" : "可编辑";
            qDebug() << "只读状态:" << stateText;
        });

        // 数值操作
        connect(m_operationButtons[3], &QPushButton::clicked, this, [this]() {
            m_dynamicSpinBox->setValue(m_dynamicSpinBox->minimum());
            qDebug() << "设置为最小值:" << m_dynamicSpinBox->minimum();
        });

        connect(m_operationButtons[4], &QPushButton::clicked, this, [this]() {
            m_dynamicSpinBox->setValue(m_dynamicSpinBox->maximum());
            qDebug() << "设置为最大值:" << m_dynamicSpinBox->maximum();
        });

        connect(m_operationButtons[5], &QPushButton::clicked, this, [this]() {
            m_dynamicSpinBox->setValue(0);
            qDebug() << "设置为0";
        });

        connect(m_operationButtons[6], &QPushButton::clicked, this, [this]() {
            int min = m_dynamicSpinBox->minimum();
            int max = m_dynamicSpinBox->maximum();
            int randomValue = QRandomGenerator::global()->bounded(min, max + 1);
            m_dynamicSpinBox->setValue(randomValue);
            qDebug() << "设置为随机值:" << randomValue;
        });

        // 信号连接
        connect(m_dynamicSpinBox, QOverload<int>::of(&ElaSpinBox::valueChanged), this, [this](int value) {
            m_statusDisplay->setText(QString("当前值: %1").arg(value));
            m_signalDisplay->setText("最后信号: valueChanged");
            qDebug() << "数值改变:" << value;
        });

        connect(m_dynamicSpinBox, &ElaSpinBox::editingFinished, this, [this]() {
            m_signalDisplay->setText("最后信号: editingFinished");
            qDebug() << "编辑完成";
        });

        // 基础功能SpinBox信号
        for (ElaSpinBox* spinBox : m_basicSpinBoxes) {
            connect(spinBox, QOverload<int>::of(&ElaSpinBox::valueChanged), this, [spinBox](int value) {
                qDebug() << spinBox->prefix() << value << spinBox->suffix() << "数值改变";
            });
        }

        // 特殊功能SpinBox信号
        connect(m_wrappingSpinBox, QOverload<int>::of(&ElaSpinBox::valueChanged), this, [this](int value) {
            qDebug() << "包装模式数值:" << value;
        });

        connect(m_keyboardTrackingSpinBox, QOverload<int>::of(&ElaSpinBox::valueChanged), this, [this](int value) {
            qDebug() << "键盘跟踪数值:" << value << "(仅在编辑完成时触发)";
        });
    }

private:
    // 模式选择
    QComboBox *m_modeComboBox;
    
    // 不同模式的SpinBox
    ElaSpinBox *m_inlineSpinBox;
    ElaSpinBox *m_compactSpinBox;
    ElaSpinBox *m_sideSpinBox;
    ElaSpinBox *m_pmSideSpinBox;
    
    // 基础功能
    ElaSpinBox *m_prefixSpinBox;
    ElaSpinBox *m_suffixSpinBox;
    ElaSpinBox *m_specialValueSpinBox;
    ElaSpinBox *m_readOnlySpinBox;
    ElaSpinBox *m_disableSpinBox;
    
    // 范围步长
    ElaSpinBox *m_rangeSpinBox;
    ElaDoubleSpinBox *m_doubleRangeSpinBox;
    ElaSpinBox *m_stepSpinBox;
    ElaSpinBox *m_accelerationSpinBox;
    
    // 特殊功能
    ElaSpinBox *m_wrappingSpinBox;
    ElaSpinBox *m_keyboardTrackingSpinBox;
    ElaSpinBox *m_alignmentSpinBox;
    
    // 动态操作
    ElaSpinBox *m_dynamicSpinBox;
    QLabel *m_statusDisplay;
    QLabel *m_signalDisplay;
    
    // 分组存储
    QList<ElaSpinBox*> m_modeSpinBoxes;
    QList<ElaSpinBox*> m_basicSpinBoxes;
    QList<ElaSpinBox*> m_rangeStepSpinBoxes;
    QList<ElaSpinBox*> m_specialSpinBoxes;
    QList<QPushButton*> m_operationButtons;
};

int main(int argc, char *argv[])
{
#if (QT_VERSION < QT_VERSION_CHECK(6, 0, 0))
    QGuiApplication::setAttribute(Qt::AA_UseHighDpiPixmaps);
#if (QT_VERSION >= QT_VERSION_CHECK(5, 14, 0))
    QGuiApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
    QGuiApplication::setHighDpiScaleFactorRoundingPolicy(Qt::HighDpiScaleFactorRoundingPolicy::PassThrough);
#else
    qputenv("QT_SCALE_FACTOR", "1.5");
#endif
#endif

    QApplication app(argc, argv);
    eApp->init();

    ElaSpinBoxDemo demo;
    demo.moveToCenter();
    demo.show();

    return app.exec();
}

#include "ElaSpinBoxTest.moc"